<?php

    require_once dirname(dirname(__FILE__)) . '/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';

    /**
     * User: Viet Anh
     * Date: 06/06/2014
     * Time: 12:06
     */
    class dao_affiliate
    {

        public function is_affiliate($user_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_user WHERE id = $user_id";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());
            $row = mysqli_fetch_array($result);

            if ($row['is_affiliate'] == 0) {
                $db->close_connect();

                return FALSE;
            } elseif ($row['is_affiliate'] == 1) {
                $db->close_connect();

                return TRUE;
            }
        }

        public function get_client_list($user_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_user WHERE invite_by_user_id = $user_id";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());

            $client_list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($client_list, $row);
            }

            $db->close_connect();

            return $client_list;
        }

        public function register_affiliate($user_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "UPDATE tbl_user SET is_affiliate = 1 WHERE id = $user_id";
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            //aff_priviledge = 1: normal access
            //aff_priviledge = 2: full access
            //default: 1
            $query1 = "INSERT INTO tbl_aff_priviledge(aff_id, aff_priviledge) VALUES ($user_id, 1)";
            mysqli_query($con, $query1) or die("Query fail: " . mysqli_error());

            $db->close_connect();

            return TRUE;
        }

        public function switch_aff_priviledge($aff_id, $aff_priviledge)
        {
            $db  = new connection();
            $con = $db->open_connect();

            //aff_priviledge = 1: normal access
            //aff_priviledge = 2: full access
            //default: 1
            $query = "UPDATE tbl_aff_priviledge SET aff_priviledge = $aff_priviledge WHERE aff_id = $aff_id";
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $db->close_connect();

            return TRUE;
        }

        public function get_aff_priviledge($aff_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_aff_priviledge WHERE aff_id = $aff_id";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());
            $row = mysqli_fetch_array($result);

            $db->close_connect();

            return $row['aff_priviledge'];
        }

        public function get_aff_priviledges()
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT ap.*, u.username AS username, u.affiliate_commission AS affiliate_commission "
                    . "FROM tbl_aff_priviledge ap "
                    . "LEFT JOIN tbl_user u ON ap.aff_id = u.id ";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());
            $list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }

            $db->close_connect();

            return $list;
        }
    }